(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.company.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'companyService' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, companyService) {
		var ctrl = this, companyInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.company = {};

		ctrl.init = function() {
			var params = {
				companyCode : companyInfo.companyCode,
				isReqiredBack : companyInfo.isReqiredBack
			};
			$scope.setPageDescription(function() {
				return ctrl.company ? ctrl.company.companyCode : null;
			});
			// init services
			companyService.initService($scope, ctrl);
			// init contacts
			companyService.initContact($scope, ctrl);	
			// init subcompany
			companyService.initSubCompany($scope, ctrl);
			// init notify tab
			companyService.initNotify($scope, ctrl);
			// init company report
			companyService.initReport($scope, ctrl);
			// init company address
			companyService.initAddress($scope, ctrl);
			// init company address
			companyService.initDynaParm($scope, ctrl);
			companyService.initEdiProfileGrid($scope, ctrl);
			//
		    ctrl.contactInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.serviceInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.subCompanyInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.notifyInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.reportInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.addressInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.selectedPropertyInnerGridOptions.enableRowHeaderSelection = false;
			ctrl.ediFileDeliInnerGridOptions.enableRowHeaderSelection = false;
			authService.view(params).then(function(response) {   
				companyService.initGeneral($scope, ctrl, response.data).then(function(){
					ctrl.company = response.data || companyInfo;
					//
					ctrl.company.companyRpts = ctrl.company.companyRpts || [];
					// init company ediprofile
					companyService.initEdiProfile($scope, ctrl);
					angular.forEach(ctrl.company.companyEdiProfile.companyEdiFileDelis, function(companyEdiProfile, key) {
						companyEdiProfile.dataStatus = 'V';
					});
					angular.forEach(ctrl.company.companyRpts, function(companyRpt, key) {
						if (!ctrl.company['companyRpts' + companyRpt.report.serviceCode]) {								
							ctrl.company['companyRpts' + companyRpt.report.serviceCode] = [];
						}
						ctrl.company['companyRpts' + companyRpt.report.serviceCode].push(companyRpt);
					});
					//init company classificationCode
					if (ctrl.classifiOptions && ctrl.classifiOptions.data && ctrl.classifiOptions.data[0]) {
						ctrl.company.classificationCode = ctrl.company.classificationCode || ctrl.classifiOptions.data[0].value;
					}
					ctrl.resetDynaProperties();
				});
			});
		}
		
	}
})(angular);